package com.common.www.bean.entity.sale;

import com.common.www.MyApplicationListener;
import org.springframework.boot.base.jpa.domain.entity.NumberIdentityEntity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import java.math.BigDecimal;

import javax.persistence.*;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 卖出单对应的实际关联的类型
 */
@DynamicUpdate(false)
@DynamicInsert(false)
@Getter
@Setter
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@NoArgsConstructor
@AllArgsConstructor
@Table()
@JsonIgnoreProperties(value = {})
public class SaleBillTypeItem extends NumberIdentityEntity {

    private static final long serialVersionUID = -2278084748058910042L;

    @JsonIgnoreProperties(value = {"typeList"})
    @ManyToOne()
    @JoinColumn(name = "parent")
    private SaleBill saleBill;

    /**
     * 类型名称名称
     */
    @Column(length = 128)
    private String name;
    /**
     * 类型描述
     */
    @Column(length = 512)
    private String description;
    /**
     * 节点地址
     */
    @Column(length = 256)
    private String nodePath;

    /**
     * 排序数字大小，数字越小排列在越前面
     */
    @Column(length = 30)
    private Integer sort;
    /**
     * 节点等级
     */
    @Column(length = 30)
    private Integer level;
    /**
     * 实际单价 元/kg 或元/吨 等，根据物品的类型来定
     */
    @Column(precision = 23, scale = 2)
    private BigDecimal price;
    /**
     * 单位类型 1.元/吨 2.元/KG 3.个
     */
    @Column(length = 11)
    private Integer unitType;
//    /**
//     * 价格的单位
//     */
//    @Column(length = 11)
//    private String priceUnit;
    /**
     * 实际称重重量
     */
    @Column(precision = 23, scale = 2)
    private BigDecimal weightActual;
    /**
     * 实际总卖价
     */
    @Column(precision = 23, scale = 2)
    private BigDecimal priceActual;
    /**
     * 配图的OSS对象名称
     */
    @Column(length = 128)
    private String img;


    @Transient
    private String unitTypeName;


    public Integer getSort() {
        if (sort == null)
            sort = 0;
        return sort;
    }

    public String getDownloadUrl() {
        return MyApplicationListener.projectPath + img;
    }

    public String getUnitTypeName() {
        if (unitType == null) {
            return "未知";
        }
        if (unitType == 1) {
            unitTypeName = "元/吨";
        } else if (unitType == 2) {
            unitTypeName = "元/KG";
        } else if (unitType == 3) {
            unitTypeName = "元/个";
        }
        return unitTypeName;
    }
}
